package MineGod;

import org.lwjgl.opengl.GL11;
import org.newdawn.slick.Image;
import org.newdawn.slick.geom.Rectangle;
import org.newdawn.slick.opengl.Texture;

public class ProjectileEntitySmall extends ProjectileEntity{
	public static Texture projectileTex;
	private Image img;
	
	private int rotSpeed;
	
	public static final int shotSpacing = 60;
	public static int lastShot = shotSpacing + 1;
	public static int LAZER_PAUR = 40;
	
	
	public ProjectileEntitySmall(double startX, double startY){
		super(ProjectileType.small, startX, startY);
		this.img = new Image(projectileTex);
		img.setCenterOfRotation(8, 8);
		rotSpeed = Chunk.rand.nextInt(10)+1;
		width = 8.0;
		height = 8.0;
		maskWidth = 6.0;
		maskHeight = 6.0;
		xSpeed = 0;
		ySpeed = 0;
		speed = 0.3;
		maxAge = 1500;
		age = 0;
		baseDamage = 10;
		mask = new Rectangle((float)(xPos+(width-maskWidth)/2), (float)(yPos+(width-maskHeight)/2), (float)maskWidth, (float)maskHeight);
	}
	
	
	public ProjectileEntitySmall shoot(int mouseX, int mouseY, double playerX, double playerY, double playerXSpeed, double playerYSpeed){
		float a = (float)playerX-(float)mouseX;
		float o = (float)playerY-(float)mouseY;
		double theta =  Math.atan((double)(o/a));
		float xs = (float)Math.cos(theta);
		if (a < 0){
			xs = Math.abs(xs);
		}
		else{
			xs = -Math.abs(xs);
		}
		float ys = (float)Math.sin(theta);
		if (o < 0){
			ys = Math.abs(ys);
		}
		else{
			ys = -Math.abs(ys);
		}
		xSpeed = (xs*speed)+playerXSpeed;
		ySpeed = (ys*speed)+playerYSpeed;
		lastShot = 0;
		return this;
	}
	
	
	public void doLogic(int delta, Chunk prevChunk, Chunk currChunk, Chunk nextChunk){
		img.rotate((float)rotSpeed/10*delta);
		xPos += xSpeed*delta;
		yPos += ySpeed*delta;
		if (yPos+height > MineGod.gameHeight){
			this.dead = true;
			return;
		}
		age += delta;
		mask.setLocation((float)(xPos + (maskWidth-width)/2), (float)(yPos+(maskHeight-height)/2));
		
		handleCollisions(delta, prevChunk, currChunk, nextChunk);
		
	}
	
	
	public Rectangle getNextMask(float xs, float ys){
		return new Rectangle((float)(xPos+(width-maskWidth)/2) + xs, (float)(yPos+(height-maskHeight)/2) + ys, (float)(maskWidth), (float)maskHeight);
	}
	
	
	public void render(double worldPos){
		if (!dead){
			GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST);
			//GL11.glDepthFunc();
			//double renderX = xPos - worldPos;
			//GL11.glColor3d(1.0,1.0,1.0);
			//img.getTexture().bind();
			//GL11.glBegin(GL11.GL_QUADS);
				GL11.glDepthFunc(GL11.GL_ALWAYS);
				img.draw((float)(xPos-worldPos),(float)yPos);
				GL11.glDepthFunc(GL11.GL_LESS);
				//GL11.glNormal3d(0, 0, 1);
				//GL11.glTexCoord2d(0.0, 0.0);
				//GL11.glVertex3d(renderX, yPos, zIndex);
				//GL11.glTexCoord2d(1.0, 0.0);
				//GL11.glVertex3d(renderX + width, yPos, zIndex);
				//GL11.glTexCoord2d(1.0, 1.0);
				//GL11.glVertex3d(renderX + width, yPos + width, zIndex);
				//GL11.glTexCoord2d(0.0, 1.0);
				//GL11.glVertex3d(renderX, yPos + width, zIndex);
			//GL11.glEnd();
		}
	}
	
	
	public void blowUp(BlockMask b){
		b.block.durability -= LAZER_PAUR;
		if (b.block.durability<=0.5){
			Utils.changeToAirBlock(b.xPos, b.yPos, b.block);
			EmitterBlockBreak e = new EmitterBlockBreak(b.xPos-MineGod.worldPosition+Block.WIDTH/2, b.yPos+Block.WIDTH/2, b.block.getTexture());
			e.start();
			MineGod.emitters.add(e);
		}
		for(BlockMask bMask: Utils.getBlockRing(b.xPos, b.yPos, 1)){
			if (bMask.block.isSolid){
				bMask.block.durability -= LAZER_PAUR/2;
				if (bMask.block.durability<=0.5){
					Utils.changeToAirBlock(bMask.xPos, bMask.yPos, bMask.block);
				}
			}
		}
		//age = maxAge+1;
	}
	
	
	
}
